<template>
  <div>
    <el-tabs type="border-card">
      <el-tab-pane label="编辑单元">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form
              :model="ruleForm"
              :rules="rules"
              ref="ruleForm"
              label-width="100px"
              class="demo-ruleForm"
              style="margin-top: 20px"
            >
              <el-form-item label="小区名称" prop="communityname">
                <el-input v-model="ruleForm.communityname"></el-input>
              </el-form-item>

              <el-form-item label="单元名称" prop="unitname">
                <el-input v-model="ruleForm.unitname"></el-input>
              </el-form-item>

              <el-form-item label="栋数" prop="unitnum">
                <el-input v-model="ruleForm.unitnum" type="number"></el-input>
              </el-form-item>

              <el-form-item label="状态选择" prop="unitstatus">
                <el-select
                  v-model="ruleForm.unitstatus"
                  placeholder="请选择状态"
                >
                  <el-option label="已建成" :value="1"></el-option>
                  <el-option label="未建成" :value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
        <vue-ueditor-wrap
          v-model="ruleForm.unitcontext"
          :config="myConfig"
          style="margin: 20px 0 0 20px; width: 95%"
        />
      </el-tab-pane>
    </el-tabs>
    <div class="fanhuik">
      <el-button @click="fanhui" class="buttonn" type="primary"
        ><i class="el-icon-document-add"></i>返回</el-button
      >
      <el-button @click="baocun" class="buttonn" type="primary"
        ><i class="el-icon-top-left"></i>保存</el-button
      >
    </div>
  </div>
</template>

<script>
import VueUeditorWrap from "vue-ueditor-wrap";

export default {
  components: { VueUeditorWrap },
  data() {
    return {
      ruleForm: {
        communityname: "",
        unitname: "",
        unitnum: "",
        unitstatus: "",
        token: "",
        unitcontext: "",
        id: "",
      },

      rules: {
        communityname: [
          { required: true, message: "请输入小区名称", trigger: "blur" },
        ],
        unitname: [
          { required: true, message: "请输入单元名称", trigger: "blur" },
        ],
        unitnum: [{ required: true, message: "请输入栋数", trigger: "blur" }],
        unitstatus: [
          { required: true, message: "请选择状态", trigger: "change" },
        ],
      },
      myConfig: {
        UEDITOR_HOME_URL: "/UEditor/",
        initialFrameWidth: "100%",
        initialFrameHeight: 300,

        imageUrl: "/api/upload/image",
        imagePath: "/",
        imageAllowFiles: [".png", ".jpg", ".jpeg", ".gif", ".bmp"],

        fileUrl: "/api/upload/file",
        filePath: "/",
        fileAllowFiles: [".doc", ".docx", ".xls", ".xlsx", ".pdf", ".zip"],
        autoHeightEnabled: false,
      },
    };
  },
  created() {
    this.ruleForm.token = localStorage.getItem("token");
    this.ruleForm.id = this.$route.query.id;
    this.getxiang(this.ruleForm.id);
  },
  methods: {
    baocun() {
      this.getbao();
    },
    async getbao() {
      let res = await this.$axios.post(
        `http://community.byesame.com/house/updateunitList`,
        {
          communityname: this.ruleForm.communityname,
          token: this.ruleForm.token,
          unitcontext: this.ruleForm.unitcontext,
          u_id: this.ruleForm.id,
          unitname: this.ruleForm.unitname,
          unitnum: this.ruleForm.unitnum,
          unitstatus: this.ruleForm.unitstatus,
        }
      );
      console.log("更新成功", res);
      this.$message({
        message: "修改成功",
        type: "success",
      });
      this.$router.push("/house/unit");
    },
    async getxiang(id) {
      let res = await this.$axios.get(
        `http://community.byesame.com/house/getDetailId?id=${id}&token=${this.ruleForm.token}`
      );
      console.log(res);
      this.ruleForm.communityname = res.data[0].communityname;
      this.ruleForm.unitname = res.data[0].unitname;
      this.ruleForm.unitnum = res.data[0].unitnum;
      this.ruleForm.unitstatus = res.data[0].unitstatus;
      this.ruleForm.unitcontext = res.data[0].unitcontext;
    },
    fanhui() {
      this.$router.push("/house/unit");
    },
  },
};
</script>

<style scoped>
.demo-ruleForm {
  padding-right: 20px;
}
.fanhuik {
  position: absolute;
  right: 1.25rem;
  z-index: 999;
  top: 8rem;
}
.fanhuik .buttonn {
  display: block;
  margin-top: 1.25rem;
  margin-left: 0;
}
</style>